package servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import basededatos.DAO;
import com.google.gson.Gson;
import dominio.Paquete;

@SuppressWarnings("serial")
@WebServlet("/CalcularPrecioReservaPaquete")
public class ServletCalcularPrecioReservaPaquete extends HttpServlet{

	
	public ServletCalcularPrecioReservaPaquete(){
		super();
	}
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		DAO dao = new DAO();
		ArrayList<Paquete> paquetes;

		float precioPaquete = 0;
		int cantClientes=Integer.parseInt(request.getParameter("cantClientes"));
		
		String json = new Gson().toJson("");
		
		try{
			
			paquetes = dao.ConsultarPaquetes();
			Iterator <Paquete> iteradorPaquetes = paquetes.iterator();
			
			while(iteradorPaquetes.hasNext()){
				Paquete paquete = iteradorPaquetes.next();

				if (paquete.getId() == Integer.parseInt(request.getParameter("idPaquete"))){
					precioPaquete = dao.ConsultarPrecioPaquete(paquete.getId());
				}

		}
		
		} catch (SQLException e) {
			e.printStackTrace();
		} catch(NumberFormatException e){
			
		}
		finally{
			json = new Gson().toJson(precioPaquete* cantClientes);
			response.setContentType("application/json"); 
			response.setCharacterEncoding("UTF-8"); 
			response.getWriter().write(json);
		}
		
	}
	
}
